Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use dataclass(frozen=True) for compatibility with Python 3.13 (incomplete, needs help) #2037

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

simonw
Copy link
Contributor

@simonw simonw commented Jul 13, 2024

@simonw simonw changed the title Python 3.13 Use dataclass(frozen=True) for compatibility with Python 3.13 Jul 13, 2024
@simonw
Copy link
Contributor Author

simonw commented Jul 13, 2024

Interesting test failure from docbuild here:

Document: getting/tutorial
--------------------------
**********************************************************************
File "getting/tutorial.rst", line 78, in default
Failed example:
    speed = 23 * ureg.snail_speed
Expected:
    Traceback (most recent call last):
    ...
    UndefinedUnitError: 'snail_speed' is not defined in the unit registry
Got:
    Traceback (most recent call last):
      File "/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/doctest.py", line 1350, in __run
        exec(compile(example.source, filename, "single",
      File "<doctest default[0]>", line 1, in <module>
        speed = 23 * ureg.snail_speed
      File "<string>", line 4, in __setattr__
    dataclasses.FrozenInstanceError: cannot assign to field 'name'
**********************************************************************

@simonw
Copy link
Contributor Author

simonw commented Jul 13, 2024

I'm a bit stuck on what to do next with this one.

@simonw simonw changed the title Use dataclass(frozen=True) for compatibility with Python 3.13 Use dataclass(frozen=True) for compatibility with Python 3.13 (incomplete, needs help) Jul 13, 2024
Copy link

codspeed-hq bot commented Jul 13, 2024

CodSpeed Performance Report

Merging #2037 will not alter performance

Comparing simonw:python-3.13 (01ea994) with master (1e46b2e)

Summary

✅ 437 untouched benchmarks

@hgrecco
Copy link
Owner

hgrecco commented Jul 28, 2024

While I have been a long proponent of making exception immutable (i.e. frozen when using a dataclasses) this has bit me many times. Do you know what is the change in Python 3.13 that makes this required?

@LecrisUT
Copy link

See: https://github.com/python/cpython/blame/3.13/Lib/dataclasses.py#L1035-L1044
It seems that was always meant to be like that, but it was not correctly checked before for multiple inheritance

@martinhoyer
Copy link

I've tried to play with it, but haven't found any good solution with keeping the dataclasses frozen in errors.py. Even if you use functools.cached_property, or other way to avoid mutation within, the errors will eventually get mutated, setting __cause__, etc. How does freezing a class that inherits non-frozen one supposed to work?

I'm not experienced enough to be able to help here, sorry.
Wouldn't using __slots__ make sense?

@LecrisUT
Copy link

@hgrecco would it be ok to move them all to frozen=False in order to unblock this issue? The doctest failure is fairly weird, because if you simply raise pint.errors.UndefinedUnitError("anything") you do not get the same traceback issue. I couldn't find the cause of this issue either. What I was able to find is that ureg.Quantity("10 * snail_speed") does output the correct exception 🤷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python 3.13 (dev) bug: cannot inherit frozen dataclass from a non-frozen one
4 participants